const express = require("express");
const route = express.Router();
const adminModel = require('../db/modle/adminModel')
const md5 = require('md5')
const {jwtEncode} = require('../utils/jwt')
route.post("/login", async (req, res) => {
    const {
        adminName,
        password
    } = req.body;
    try {
        //对数据库进行查询
        const loginRe = await adminModel.findOne({
            adminName
        });

        //账号不存在
        if (!loginRe) {
            return res.send({
                code: 10002,
                msg: "账号不存在",
                data: null
            })
        }
        if (loginRe.password !== md5(password+66)) {
            return res.send({
                code: 10003,
                msg: "密码错误",
                data: null
            })
        }
        res.send({
            code: 10000,
            msg: "登录成功",
            data: {
              token: jwtEncode(loginRe.adminName,'123')
            }
        })
    } catch (e) {
        res.send({
            code: 10004,
            message: "其他类型错误" + e.message,
            data: null
        })
    }
})
module.exports = route;